package mipo.top.pet.Service.impl;

import mipo.top.admin.domain.Department;
import mipo.top.base.service.impl.BaseServiceimpl;
import mipo.top.pet.Service.IPetTypeService;
import mipo.top.pet.domain.PetType;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class PetTypeServiceimpl extends BaseServiceimpl<PetType> implements IPetTypeService {
    @Override
    public List<PetType> petTree() {
        List<PetType> alldept = super.findall();
        Map<Long, PetType> map = alldept.stream().collect(Collectors.toMap(PetType::getId, e -> e));
        List<PetType> treeData = new ArrayList<>();
        for (PetType petType : alldept) {
            if(petType.getPid()== null){
                treeData.add(petType);
            }else {
                PetType sd = map.get(petType.getPid());
                if(sd != null) {
                    sd.getChildren().add(petType);
                }
            }
        }
        return treeData;
    }
}
